package com.hisense.ovcloud.mqtt.warehouse.service;

import com.hisense.ovcloud.commons.configs.base.BaseResponse;
import com.hisense.ovcloud.commons.dto.req.CreateProductReq;
import com.hisense.ovcloud.mqtt.warehouse.Constants;
import com.hisense.ovcloud.mqtt.warehouse.entity.device.ProductInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.time.Instant;
import java.util.Date;

@Service
@Slf4j
public class ProductService {
    public Mono<BaseResponse> createProduct(Mono<CreateProductReq> req) {
        return req.flatMap(r -> Mono.just(BaseResponse.builder().data(
                ProductInfo.model()
                        .setDomain(r.getMajorDomainId())
                        .setSubDomainId(r.getSubDomainId())
                        .setName(r.getName())
                        .setType(r.getType())
                        .setModel(r.getModel())
                        .setOs(r.getOs())
                        .setCommunication(r.getCommunication())
                        .setProtocol(r.getProtocol())
                        .setTransportProtocol(r.getTransportProtocol())
                        .setSecType(r.getSecType())
                        .setLicenseMode(r.getLicenseMode())
                        .setThirdCloud(r.getThirdCloud())
                        .setDescription(r.getDescription())
                        .setCategory(r.getCategory())
                        .setProductImageUrl(r.getProductImageUrl())
                        .setDepartmentId(r.getDepartmentId())
                        .setManufacture(r.getManufacture())
                        .setDeviceUplinkUrl(Constants.Product.CANNOT_BE_EMPTY)
                        .setCreateTime(Date.from(Instant.now()))
                        .setModifyTime(Date.from(Instant.now()))
                        .setFeaturecode(r.getFeaturecode())
                        .setProductType(r.getVariety())
                        .setBrand(r.getBrand())
                        .saveOpt()
                        .orElseThrow(RuntimeException::new)
                ).build())
        );
    }
}
